<template>
  <el-card class="box-card">
    <div class="model-role">
      <el-form :inline="true" :model="dataForm" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="dataForm.customerName" clearable placeholder="客户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.attribution" clearable placeholder="所属区域"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.accountManager" clearable placeholder="客户经理"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDateList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="openDialog()">添加客户</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="dataList" border style="width: 100%">
        <el-table-column prop="customerName" label="客户姓名" width="150"></el-table-column>
        <el-table-column prop="address" label="客户地址" width="150"></el-table-column>
        <el-table-column prop="attributionName" label="区域" width="150"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="150"></el-table-column>
        <el-table-column prop="phone" label="手机号" width="150"></el-table-column>
        <el-table-column prop="accountManagerName" label="客户经理" width="150"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[5,10,20]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total,sizes,prev,pager,next,jumper"
        style="margin-top: 30px">
      </el-pagination>
    </div>

     <el-dialog :title="dataDialogForm.id === '' ?  dialogTitle:dialogTitle='更新客户信息' " :visible.sync="dialogFormVisible" @close="closeDialog()">
       <el-form :model="dataDialogForm" :rules="rules" ref="ruleForm">
          <el-form-item label="客户名称" :label-width="formLabelWidth">
            <el-input v-model="dataDialogForm.customerName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="客户地址" :label-width="formLabelWidth" autocomplete="off">
            <el-input v-model="dataDialogForm.address"></el-input>
          </el-form-item>
         <el-form-item label="客户所属地区" :label-width="formLabelWidth">
           <el-select v-model="dataDialogForm.attribution" placeholder="请选择区域" style="width: 100%">
             <el-option
               v-for="item in regionList"
               :key="item.id"
               :label="item.area"
               :value="item.id">
             </el-option>
           </el-select>
         </el-form-item>
         <el-form-item label="邮箱" :label-width="formLabelWidth">
            <el-input v-model="dataDialogForm.email" autocomplete="off"></el-input>
         </el-form-item>
         <el-form-item label="手机号" :label-width="formLabelWidth" prop="phone">
            <el-input v-model="dataDialogForm.phone" autocomplete="off"></el-input>
         </el-form-item>
         <el-form-item label="客户经理" :label-width="formLabelWidth">
          <el-select v-model="dataDialogForm.accountManager" placeholder="请选择经理" style="width: 100%">
             <el-option
               v-for="item in managerList"
               :key="item.id"
               :label="item.username"
               :value="item.id">
             </el-option>
           </el-select>
         </el-form-item>
         </el-form>
       <div slot="footer" class="dialog-footer">
         <el-button @click="dialogFormVisible = false">取 消</el-button>
         <el-button type="primary" @click="handleConfirm()">确 定</el-button>
       </div>
      </el-dialog>

  </el-card>
</template>

<script>

export default {
  name: 'customer',
  data() {
    
    return {
      dialogTitle: '添加客户',
      dataForm: {
        customerName: "",
        attribution: "",
        accountManager: "",
      },
      ruleForm:{
        pass: '',
      },
      dataDialogForm: {
        id: '',
        customerName:'',
        address:'',
        attribution:'',
        email:'',
        phone:'',
        accountManager:'',
        createTime:''
      },
      regionList:[],
      managerList:[],
      dataList:[],
      userName: "xiaohei",
      pageIndex: 1,
      pageSize:5,
      totalPage:0,
      datalistLoading:false ,
      dialogFormVisible:false,
      dialogFormSubmitVisible:false,
      formLabelWidth: '120px',
      rules:{
        phone:[{required:true ,message:'请输手机号',trigger:'blur'},{pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号'},]
      }
    };
  },
  methods:{
    sizeChangeHandle(val){
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDateList()
    },
    currentChangeHandle(val){
      this.pageIndex = val
      this.getDateList()
    },
    getDateList() {
      // this.datalistLoading = true
      //生命params 分页查询的参数
      const params = {
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          customerName: this.dataForm.customerName,
          attribution: this.dataForm.attribution,
          accountManager: this.dataForm.accountManager
        }
      }
      this.$http.get('/customer/findByPage', params).then((res) => {
        console.log(res)
        this.dataList = res.data.data.records
        this.totalPage = res.data.data.current
      })
    },
    handleEdit(index,item){
      //打开更新窗口
      this.dataDialogForm.id = item.id
      this.dataDialogForm.customerName = item.customerName
      this.dataDialogForm.address = item.address
      this.dataDialogForm.attribution = item.attribution
      this.dataDialogForm.email = item.email
      this.dataDialogForm.phone = item.phone
      this.dataDialogForm.accountManager = item.accountManager
      this.dataDialogForm.createTime = item.createTime
      this.openDialog();

      console.log('item',item)
    },
    handleDelete(index, item){
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete('/customer/'+item.id).then((res)=>{
          if (res.data.code == 200) {
            this.getDateList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          } else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            })
          }
        })
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //添加用户按钮绑定的方法
    openDialog(){
      //打开窗口
      this.dialogFormVisible = true
    },
    closeDialog(){
      this.dialogFormVisible = false;
      this.dialogTitle = '添加客户'

      //所有字段置空
      this.dataDialogForm = {
        id: '',
        customerName: '',
        address: '',
        attribution: '',
        email: '',
        phone: '',
        accountManager: '',
        createTime: ''
      }
    },
    handleConfirm() {
      // 当点击确定按钮时，访问当前对话框的标题
      const title = this.dialogTitle;
      console.log('当前对话框标题:', title);

      if (title === '添加客户') {
        this.handleSubmitFormData('ruleForm');
      } else {
        this.handleUpdateFormData('ruleForm');
      }

    },
    handleSubmitFormData(ruleForm){
      this.addCustomer(ruleForm)
    } ,
    handleUpdateFormData(ruleForm){
      this.updateCustomer(ruleForm)
    },
    updateCustomer(ruleForm){
      this.$refs[ruleForm].validate((valid) => {
        this.$http.put('/customer',this.dataDialogForm).then((res)=>{
          console.log(res)
          this.dialogFormVisible = false   //关闭窗口
          this.closeDialog()
          this.getDateList()
        })
      });
    },
    addCustomer(ruleForm){
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          console.log(this.dataDialogForm)
            this.$http.post('/customer',this.dataDialogForm).then((res)=>{
              console.log(res)
              this.dialogFormVisible = false   //关闭窗口
              this.closeDialog()
              this.getDateList()
            })
            return
        } else {
          return false;
        }
      });
      
    },
    getRegionList() {
      const params = {params:{pageIndex:1,pageSize:1000}}
      this.$http.get('/region/findByPage',params).then((res)=>{
        console.log(res)
        this.regionList = res.data.data.records
      })
    },
    getManagerList() {
      const params = {params:{pageIndex:1,pageSize:1000}}
      this.$http.get('/user/findByPage',params).then((res)=>{
        console.log(res)
        this.managerList = res.data.data.records
      })
    },
  },
  mounted() {
    this.getDateList()
    this.getRegionList()
    this.getManagerList()
  },
}

</script>
